package com.itheima.controller;


import com.itheima.common.R;

import com.itheima.pojo.dto.setmealDto.SetmealAddDto;

import com.itheima.pojo.dto.setmealDto.SetmealUpdateDto;

import com.itheima.pojo.vo.setmealVo.SetmealByIdVo;

import com.itheima.service.SetmealService;

import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.text.ParseException;
import java.util.HashMap;

import java.util.Map;

@RestController
@RequestMapping("/setmeal")
public class SetmealController {
    @Resource
    private SetmealService setmealService;

    @PostMapping
    public R<String> add(@RequestBody SetmealAddDto setmealDishes){

        boolean add = setmealService.add(setmealDishes);
        if (add){
            return R.success("添加成功");
        }
        return R.error("添加失败");
    }
    @PutMapping
    public R<String> update(@RequestBody SetmealUpdateDto setmeal){

        boolean update=setmealService.update(setmeal);

        if (update){
            return R.success("修改成功");
        }
        return R.error("修改失败");
    }
    @DeleteMapping
    public R<String> delete(String[]ids){

        for (String id : ids) {
            Integer status = setmealService.selectById(id).getStatus();
            if (status==1){
                return R.error("套餐正在售卖中，不能删除");
            }else if (setmealService.delete(ids)){

                return R.success("删除成功");
            }

        }
        return R.error("删除失败");
    }
    @GetMapping("/page")
    public R<Object> selectByPage(String name,Integer page,Integer pageSize) throws ParseException {

        Integer total=(page-1)*pageSize;
        Map<String, Object> map = new HashMap<>();
        map.put("records", setmealService.selectByPage(name, page, pageSize));
        map.put("total", total);
        map.put("size", pageSize);
        map.put("current", page);

        map.put("optimizeCountSql", true);
        map.put("hitCount", false);
        map.put("countId", null);
        map.put("maxLimit", null);
        map.put("searchCount", true);
        map.put("pages", page);
        return R.success(map);
    }
    @PostMapping("/status/{num}")
    public R<String> update(@PathVariable("num") int num,@RequestParam String[]ids){
        boolean result = setmealService.status(num,ids);
        if (result) {
            return R.success("修改套餐成功");
        }
        return R.error("修改套餐失败");
    }
    @GetMapping("/{id}")
    public R<Object> selectById(@PathVariable String id){
        SetmealByIdVo setmealByIdVo = setmealService.selectById(id);
        return  R.success(setmealByIdVo);
    }


}
